import React from 'react';
import { Input, InputNumber, Tooltip } from 'antd';
import { isEqual, isFunction, isNumber, omit, trim } from 'lodash-es';
import PropTypes from 'prop-types';
import BaseInputField from '../Base/BaseInputField';
import { formatMoney, isNotEmpty, sleepFunc } from 'jh-utils';
import { previewNumber } from '../Base/helper/compValuePreview';

// export function formatNumber(value) {
//     value += '';
//     const list = value.split('.');
//     const prefix = list[0].charAt(0) === '-' ? '-' : '';
//     let num = prefix ? list[0].slice(1) : list[0];
//     let result = '';
//     while (num.length > 3) {
//         result = `,${num.slice(-3)}${result}`;
//         num = num.slice(0, num.length - 3);
//     }
//     if (num) {
//         result = num + result;
//     }
//     return `${prefix}${result}${list[1] ? `.${list[1]}` : ''}`;
// }

export default class NumberBase extends BaseInputField {

    static propTypes = {
        ...BaseInputField.propTypes,
        value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
        defaultValue: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
    };

    hdlValueChange(e) {
        const maxVal = 99999999999999;
        const minVal = -99999999999999;
        let { max = maxVal, min = minVal } = this.getProps();
        if (!isNotEmpty(max)) max = maxVal;
        if (!isNotEmpty(min)) min = minVal;

        max > maxVal && (max = maxVal);
        min < minVal && (min = minVal);
        // console.log('change', e)
        const reg = /^-?[0-9]*(\.[0-9]*)?$/;
        let fValue = this.state.value;
        let value = `${e && e.target ? e.target.value : e}`;
        value = value.replace(/,/g, '');

        if ((!isNaN(value) && reg.test(value)) || value === '' || value === '-') {
            fValue = value.replace(/0*(\d+)/, '$1');
        }

        if (fValue > max) {
            fValue = max;
        }

        if (fValue < min) {
            fValue = min;
        }

        if (!isEqual(fValue, this.state.value)) {
            super.hdlValueChange(fValue);
        }
    }

    // '.' at the end or only '-' in the input box.
    hdlValueBlur(e) {
        // console.log('hdlValueBlur');
        let { value } = this.state;
        let { dataType } = this.props;

        if (dataType && dataType === 'int') {
            value = Math.round(value);
        }

        if (isNumber(value)) {
            value = value.toString();
        }

        let valueTemp = value;

        if ('undefined' !== typeof value && null !== value) {
            if (value.charAt(value.length - 1) === '.' || value === '-') {
                valueTemp = value.slice(0, -1);
            }
            valueTemp = valueTemp.replace(/0*(\d+)/, '$1');
            this.runPropsFunc('onChange')(valueTemp, false);
            this.runPropsFunc('onBlur')(e, valueTemp);
        }
        // 每次失去焦点的时候，触发 afterCbs
        this.runAfterCbs(valueTemp);
        this.setState({ focusing: false });
    }

    mainRender(context) {
        const props = this.props;
        const { floatNum, format } = props;
        const { value, focusing } = this.state;
        let showValue = previewNumber(value, floatNum, format);
        const title = isNotEmpty(value) ? (
            <span className="numeric-input-title">{value !== '-' ? showValue : '-'}</span>
        ) : (
            '-'
        );
        const moreProps = omit(props, [...this.commonProps, 'defaultOpen', 'onChange', 'max', 'min']);
        if (!focusing && (format || floatNum > 0)) {
            moreProps.formatter = (value) => ('' === trim(value) ? '' : previewNumber(value, floatNum, format));
        }
        return (
            <Tooltip
                trigger={'focus'}
                title={title}
                placement="topLeft"
                overlayClassName="numeric-input"
            >
                <InputNumber
                    title={showValue}
                    allowClear={true}
                    {...moreProps}
                    value={value}
                    onChange={this.hdlValueChange.bind(this)}
                    onBlur={this.hdlValueBlur.bind(this)}
                    onFocus={this.hdlValueFocus.bind(this)}
                    ref={this.refFunc.bind(this)}
                />
            </Tooltip>
        );
    }
}